package com.xyazm.pay.api;

import com.xyazm.common.core.JsonResult;
import com.xyazm.common.utils.ParamCheckUtil;
import com.xyazm.pay.domain.dto.PayOrderDTO;
import com.xyazm.pay.domain.param.PayOrderParam;
import com.xyazm.pay.domain.param.PayRefundParam;
import com.xyazm.pay.domain.param.QueryTradeNoParam;
import com.xyazm.pay.exception.PayBizException;
import com.xyazm.pay.service.IPayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 支付服务对外暴露的接口实现类
 * @author xuyou
 * 2022/6/8 0008 21:46
*/
@Slf4j
@DubboService(version = "1.0.0",interfaceClass = IPayApi.class)
public class PayApi implements IPayApi {

    @Autowired
    private IPayService payService;

    @Override
    public JsonResult<PayOrderDTO> payOrder(PayOrderParam param) {
        try {
            ParamCheckUtil.checkObjectNonNull(param);
            PayOrderDTO result = payService.payOrder(param);
            return JsonResult.buildSuccess(result);
        } catch (PayBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<Void> executeRefund(PayRefundParam param) {
        try {
            payService.executeRefund(param);
            return JsonResult.buildSuccess();
        } catch (PayBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
